package com.google.denta.service;

import java.util.List;

import org.apache.commons.configuration.Configuration;

import com.google.denta.dao.GroupDao;
import com.google.denta.dao.UserDao;
import com.google.denta.entity.Group;
import com.google.denta.entity.Role;
import com.google.denta.entity.User;

/**
 * Group service implementation.
 * @author Aliaksei_Kuliashou
 */
public class GroupServiceImpl implements GroupService {

    /** Group dao. */
    private GroupDao groupDao;

    /** User dao. */
    private UserDao userDao;

    /** Configuration. */
    private Configuration conf;

    /**
     * Setter for groupDao.
     * @param groupDao - the groupDao to set.
     */
    public void setGroupDao(final GroupDao groupDao) {
        this.groupDao = groupDao;
    }

    /**
     * Setter for userDao.
     * @param userDao - the userDao to set.
     */
    public void setUserDao(final UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * @param conf the conf to set
     */
    public void setConf(Configuration conf) {
        this.conf = conf;
    }

    /** {@inheritDoc} */
    public List<Group> getGroups() {
        return groupDao.findAll();
    }

    /** {@inheritDoc} */
    public Group getGroup(final Long id) {
        return groupDao.read(id);
    }

    /** {@inheritDoc} */
    public Group getGroupByName(final String name) {
        return groupDao.findByName(name);
    }

    /** {@inheritDoc} */
    public void deleteGroup(final Long id) {
        groupDao.deleteGroupUsers(id);
        groupDao.deleteGroupRoles(id);
        groupDao.delete(id);
    }

    /** {@inheritDoc} */
    public void deleteGroups(final Long[] ids) {
        if (ids != null) {
            for (Long id : ids) {
                deleteGroup(id);
            }
        }
    }

    /** {@inheritDoc} */
    public void saveGroup(final Group group) {
        if (group.getId() == null) {
            Long id = groupDao.create(group);
            group.setId(id);
        } else {
            groupDao.update(group);
        }
        saveGroupRoles(group);
    }

    /** {@inheritDoc} */
    public void saveGroupRoles(final Group group) {
        groupDao.deleteGroupRoles(group.getId());
        if (group.getRoles() != null) {
            for (Role role : group.getRoles()) {
                groupDao.createGroupRole(group.getId(), role.getId());
            }
        }
    }

    /** {@inheritDoc} */
    public List<User> getUsers(Long id, Boolean flag) {
        if (flag) {
            return userDao.findByGroup(id, true);
        } else {
            return userDao.findByGroup(id, null);
        }
    }

    /** {@inheritDoc} */
    public void deleteUsers(Long gid) {
        groupDao.deleteGroupUsers(gid);
    }

    /** {@inheritDoc} */
    public void deleteUsers(Long gid, Long[] ids) {
        if ((ids != null) && (ids.length > 0)) {
            for (Long uid : ids) {
                groupDao.deleteGroupUser(gid, uid);
            }
        }
    }

    /** {@inheritDoc} */
    public void addUsers(Long gid, Long[] uids) {
        if ((uids != null) && (uids.length > 0)) {
            for (Long uid : uids) {
                groupDao.addGroupUser(gid, uid);
            }
        }
    }

    /** {@inheritDoc} */
    public Long getPagesItems() {
        return conf.getLong("employees.on.page");
    }
}
